<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\MoneyLog;
use app\common\model\User;
use think\Db;
use think\Exception;

class UserMoneyManager extends BaseManager
{
    CONST TYPE_INCREASE = 2;
    CONST TYPE_DECREASE = 1;

    /**
     * 金额变动类型 :
     * 1 = 充值
     * 2 = 冻结
     * 3 = 提现
     * 4 = 组队 [41=收入,42=支出]
     * 5 = 比赛
     * 6 = 购买体验卡
     * 7 = 解冻返回
     */
    CONST TYPE_RECHARGE = 1;
    CONST TYPE_FREEZE = 2;
    CONST TYPE_WITHDRAWAL = 3;
    CONST TYPE_TEAM = 4;
    CONST TYPE_TEAM_INCREASE = 41;
    CONST TYPE_TEAM_DECREASE = 42;
    CONST TYPE_MATCH = 5;
    CONST TYPE_VOUCHER = 6;
    CONST TYPE_UNFREEZE_RETURN = 7;
    CONST TYPE_REFUND = 8;

    CONST TYPE_REWARD = 9;
    CONST TYPE_INVITED = 10;
    CONST TYPE_MATCH_RETURN = 11;
    CONST TYPE_RECHARGE_REWARD = 12;
    CONST TYPE_PROXY_RECHARGE_REWARD = 13;
    CONST TYPE_PROXY_STORE_TEAM_REWARD = 14;
    CONST TYPE_PROXY_CAPTAIN_TEAM_REWARD = 15;
    CONST TYPE_SYS_CHANGE = 16;
    CONST TYPE_ASSISTANT_REWARD = 17;
    CONST TYPE_ASSISTANT_REFUND = 18;
    CONST TYPE_RECHARGE_MESSAGE = '用户%s 于 %s 充值 %.2f';
    CONST TYPE_FREEZE_MESSAGE = '用户%s 于 %s 提现冻结 %.2f';
    CONST TYPE_WITHDRAWAL_MESSAGE = '用户%s 于 %s 提现 %.2f';
    CONST TYPE_INCREASE_TEAM_MESSAGE = '用户%s 于 %s 组队成功,收入%.2f';
    CONST TYPE_DECREASE_TEAM_MESSAGE = '用户%s 于 %s 参与组队,支出%.2f';
    CONST TYPE_MATCH_MESSAGE = '用户%s 于 %s 参加比赛,支出 %.2f ';
    CONST TYPE_VOUCHER_MESSAGE = '用户%s 于 %s 购买体验卡,支出 %.2f';
    CONST TYPE_UNFREEZE_RETURN_MESSAGE = '用户%s 于 %s 解冻返回余额 %.2f';
    CONST TYPE_REFUND_MESSAGE = '用户%s 于 %s 退款返回至余额 %.2f';
    CONST TYPE_REWARD_MESSAGE = '用户%s 于 %s 邀请用户奖励 %.2f';
    CONST TYPE_INVITED_MESSAGE = '用户%s 于 %s 被邀请奖励 %.2f';
    CONST TYPE_MATCH_RETURN_MESSAGE = '用户%s 于 %s 退款返回至余额 %.2f';
    CONST TYPE_RECHARGE_REWARD_MESSAGE = '用户%s 于 %s 充值获得奖励 %.2f';
    CONST TYPE_PROXY_RECHARGE_REWARD_MESSAGE = '代理用户%s 于 %s 获得充值奖励 %.2f';
    CONST TYPE_PROXY_STORE_TEAM_REWARD_MESSAGE = '代理用户%s 于 %s 获得门店组队奖励 %.2f';
    CONST TYPE_PROXY_CAPTAIN_TEAM_REWARD_MESSAGE = '代理用户%s 于 %s 获得用户参与组队奖励 %.2f';
    CONST TYPE_SYS_CHANGE_MESSAGE = '用户%s 于 %s 系统变更用户余额 %.2f';
    CONST TYPE_ASSISTANT_REWARD_MESSAGE = '用户%s 于 %s 获取助教分成 %.2f';
    CONST TYPE_ASSISTANT_REFUND_MESSAGE = '用户%s 于 %s 获取助教退款 %.2f';

    /**
     * @param object $userAccount 用户账户
     * @param int $userId 用户id
     * @param float $money 金额
     * @param int $outIn 1=收入,2=支出
     * @param int $detail 1 = 充值  2=冻结 3=提现 4 = 组队 [41=收入,42=支出] 5 = 比赛 6 = 购买体验卡 7=解冻返回
     * @param string $message 消息
     * @param array $extend 扩展字段
     * @param boolean $trans 是否开启事务
     * @return void
     */
    public static function UserMoneyLog(object $userAccount,int $userId, float $money,int $outIn,int $detail,string $message,array $extend=[],bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            MoneyLog::create([
                'user_id' => $userId,
                'type' => $outIn,
                'detail' => $detail,
                'money' => $money,
                'before' => $userAccount->total,
                'after' => bcadd($userAccount->total,$money,2),
                'memo' => $message,
                'relation_id' => $extend['relation_id'] ?? 0,
                'relation_order' => $extend['relation_order'] ?? '',
                'createtime' => time(),
            ]);

            if ($trans){
                Db::commit();
            }
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}